import {
	high as highImage,
	middle as middleImage,
	low as lowImage,
	addPostProcessStage,
	removePostProcessStage
} from './Utils/cesium-weather/index';
var myRain, mySnow, myFog;
export class Weather {
	constructor(viewer, iCesium) {
		this.viewer = viewer;
		this.iCesium = iCesium;
	}

	/**
	 * 获取天气参数
	 * @returns {Promise<{rain: {speed:*,density:*}, snow:{speed:*,density:*},fog:{fogByDistance:*,fogColor:*}}>}
	 */
	async getParams() {
		try {
			let rain, snow, fog;
			if (myRain) {
				const { speed, density } = myRain.uniforms;
				rain = { speed, density };
			} else {
				rain = {};
			}
			if (mySnow) {
				const { speed, density } = mySnow.uniforms;
				snow = { speed, density };
			} else {
				snow = {};
			}
			if (myFog) {
				const { fogByDistance, fogColor } = myFog.uniforms;
				fog = { fogByDistance, fogColor };
			} else {
				fog = {};
			}
			return { rain, snow, fog };
		} catch (e) {
			console.log(e.message);
		}
	}

	/**
	 * 雨效开启/关闭
	 * @param {Boolean} flag 设置值
	 */
	setRainEnabled(flag) {
		if (flag) {
			myRain = myRain || addPostProcessStage(this.viewer, 'my_rain', 'rain');
			myRain.enabled = true;
		} else {
			myRain.enabled = false;
			// removePostProcessStage(this.viewer, 'my_rain')
		}
	}

	/**
	 * 雨效速度设置
	 * @param {number} value 速度值
	 */
	setRainSpeed(value) {
		myRain ? (myRain.uniforms.speed = parseFloat(value)) : console.log('请先开启雨效果');
	}

	/**
	 * 雨效密度设置
	 * @param {number} value
	 */
	setRainDensity(value) {
		myRain ? (myRain.uniforms.density = parseFloat(value)) : console.log('请先开启雨效果');
	}

	/**
	 * 雨效设置
	 * @param {Boolean} switchValue 设置值
	 * @param {Number} density 密度值 0~1
	 * @param {Number} speed 速度值 0~1
	 */
	setRainParam(switchValue, density, speed) {
		if (switchValue) {
			myRain = myRain || addPostProcessStage(this.viewer, 'my_rain', 'rain');
			myRain.uniforms.speed = parseFloat(speed);
			myRain.uniforms.density = parseFloat(density);
			myRain.enabled = true;
		} else {
			myRain.enabled = false;
		}
	}

	/**
	 * 雪效开启/关闭
	 * @param {Boolean} flag 设置值
	 */
	setSnowEnabled(flag) {
		// this.viewer.scene.postProcessStages.snow.enabled = flag
		if (flag) {
			mySnow = mySnow || addPostProcessStage(this.viewer, 'my_snow', 'snow');
			mySnow.enabled = true;
		} else {
			mySnow.enabled = false;
			// removePostProcessStage(this.viewer, 'my_snow')
		}
	}

	/**
	 * 雪效速度设置
	 * @param {number} value 速度值
	 */
	setSnowSpeed(value) {
		mySnow ? (mySnow.uniforms.speed = parseFloat(value)) : console.log('请先开启雪效果');
	}

	/**
	 * 雪效密度设置
	 * @param {number} value 密度值
	 */
	setSnowDensity(value) {
		mySnow ? (mySnow.uniforms.density = parseFloat(value)) : console.log('请先开启雪效果');
	}

	/**
	 * 雪效设置
	 * @param {Boolean} switchValue 设置值
	 * @param {Number} density 密度值 0~1
	 * @param {Number} speed 速度值 0~1
	 */
	setSnowParam(switchValue, density, speed) {
		if (switchValue) {
			mySnow = mySnow || addPostProcessStage(this.viewer, 'my_snow', 'snow');
			mySnow.uniforms.speed = parseFloat(speed);
			mySnow.uniforms.density = parseFloat(density);
			mySnow.enabled = true;
		} else {
			mySnow.enabled = false;
		}
	}

	/**
	 * 雾效开启/关闭
	 * @param {Boolean} flag 设置值
	 */
	setFogEnabled(flag) {
		if (flag) {
			myFog = myFog || addPostProcessStage(this.viewer, 'my_fog', 'fog');
			myFog.enabled = true;
		} else {
			myFog.enabled = false;
			// removePostProcessStage(this.viewer, 'my_fog')
		}
	}

	/**
	 * 雾效范围设置
	 * @param startDistance
	 * @param endDistance
	 */
	setFogDistance(startDistance, endDistance) {
		const value = new Cesium.Cartesian4(startDistance, 0.0, endDistance, 1.0);
		myFog ? (myFog.uniforms.fogByDistance = value) : console.log('请先开启雾效果');
	}

	/**
	 * 雾效设置
	 * @param {Boolean} switchValue 开关值
	 */
	setFogParam(switchValue) {
		if (switchValue) {
			myFog = myFog || addPostProcessStage(this.viewer, 'my_fog', 'fog');
			myFog.enabled = true;
		} else {
			myFog.enabled = false;
		}
	}

	// 云开启/关闭
	setCloudEnabled(flag) {
		const cloudBox = new Cesium.CloudBox({
			url: middleImage
		});
		this.viewer.scene.cloudBox = flag ? cloudBox : null;
	}

	/**
	 * 密度切换图片
	 * @param {number} value 密度值 1/2/3
	 */
	setCloudDensity(value) {
		switch (value) {
			case 1: {
				this.viewer.scene.cloudBox.url = lowImage;
				break;
			}
			case 2: {
				this.viewer.scene.cloudBox.url = middleImage;
				break;
			}
			case 3: {
				this.viewer.scene.cloudBox.url = highImage;
				break;
			}
		}
	}

	/**
	 * 设置云效
	 * @param {Boolean} switchValue 开关值
	 * @param {Number} density 密度值 1/2/3
	 */
	setCloudParam(switchValue, density) {
		if (switchValue) {
			const cloudBox = new Cesium.CloudBox({
				url: middleImage
			});
			this.viewer.scene.cloudBox = this.viewer.scene.cloudBox || cloudBox;
			switch (density) {
				case 1: {
					this.viewer.scene.cloudBox.url = lowImage;
					break;
				}
				case 2: {
					this.viewer.scene.cloudBox.url = middleImage;
					break;
				}
				case 3: {
					this.viewer.scene.cloudBox.url = highImage;
					break;
				}
				default:
					this.viewer.scene.cloudBox.url = middleImage;
			}
		} else {
			this.viewer.scene.cloudBox = null;
		}
	}
}
